home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / packages / texnfo-tex.el.z / texnfo-tex.el
Encoding:
Text File  |  1998-05-21  |  14.1 KB  |  371 lines

  1. ;;;; texnfo-tex.el
  2.  
  3. ;;; Texinfo mode TeX and hardcopy printing commands.
  4.  
  5. ;; These commands are for running TeX on a region of a Texinfo file in
  6. ;; GNU Emacs, or on the whole buffer, and for printing the resulting
  7. ;; DVI file.
  8.  
  9. ;;; Version 2.07    22 October 1991
  10. ;;; Robert J. Chassell      
  11. ;;; Please send bug reports to:  bug-texinfo@prep.ai.mit.edu
  12.  
  13. ;;; Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
  14.  
  15.  
  16. ;; This file is part of XEmacs.
  17.  
  18. ;; XEmacs is free software; you can redistribute it and/or modify it
  19. ;; under the terms of the GNU General Public License as published by
  20. ;; the Free Software Foundation; either version 2, or (at your option)
  21. ;; any later version.
  22.  
  23. ;; XEmacs is distributed in the hope that it will be useful, but
  24. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  25. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  26. ;; General Public License for more details.
  27.  
  28. ;; You should have received a copy of the GNU General Public License
  29. ;; along with XEmacs; see the file COPYING.  If not, write to the 
  30. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  31. ;; Boston, MA 02111-1307, USA.
  32.  
  33. ;;; Synched up with: Not in FSF.
  34.  
  35.  
  36. ;;; The Texinfo mode TeX related commands are:
  37.  
  38. ; texinfo-tex-region        to run tex on the current region.
  39. ; texinfo-tex-buffer        to run tex on the current buffer.
  40. ; texinfo-texindex          to sort unsorted index files.
  41. ; texinfo-tex-print         to print the .dvi file made by tex.
  42. ; texinfo-kill-tex-job      to kill the currently running tex job.
  43. ; texinfo-recenter-tex-output-buffer    to redisplay tex output buffer.
  44. ; texinfo-show-tex-print-queue          to show the print queue.
  45.  
  46.  
  47. ;;; Keys common both to Texinfo mode and to TeX shell.
  48.  
  49. ;; Defined in `texinfo.el'
  50. ; (defun texinfo-define-common-keys (keymap)
  51. ;   "Define the keys both in Texinfo mode and in the texinfo-tex-shell."
  52. ;   (define-key keymap "\C-c\C-t\C-k"    'texinfo-kill-tex-job)
  53. ;   (define-key keymap "\C-c\C-t\C-x"    'texinfo-quit-tex-job)
  54. ;   (define-key keymap "\C-c\C-t\C-l"    'texinfo-recenter-tex-output-buffer)
  55. ;   (define-key keymap "\C-c\C-t\C-d"    'texinfo-delete-from-tex-print-queue)
  56. ;   (define-key keymap "\C-c\C-t\C-q"    'texinfo-show-tex-print-queue)
  57. ;   (define-key keymap "\C-c\C-t\C-p"    'texinfo-tex-print)
  58. ;   (define-key keymap "\C-c\C-t\C-i"    'texinfo-texindex)
  59. ;   (define-key keymap "\C-c\C-t\C-r"    'texinfo-tex-region)
  60. ;   (define-key keymap "\C-c\C-t\C-b"    'texinfo-tex-buffer))
  61.  
  62. ;; See also texinfo-tex-start-shell. 
  63. ;; The following is executed in the `texinfo.el' file
  64. ;(texinfo-define-common-keys texinfo-mode-map)
  65.  
  66.  
  67. ;;; Variable definitions:
  68.  
  69. (defgroup texinfo-tex nil
  70.   "TeX and hardcopy printing commands for Texinfo mode"
  71.   :prefix "texinfo-"
  72.   :group 'tex
  73.   :tag "Texinfo TeX"
  74.   :group 'texinfo)
  75.  
  76. (require 'shell)
  77.  
  78. (defcustom texinfo-tex-shell-cd-command "cd"
  79.   "Command to give to shell running TeX to change directory."
  80.   :type 'string
  81.   :group 'texinfo-tex)
  82.  
  83. (defcustom texinfo-tex-command "tex"
  84.   "*Command used by  texinfo-tex-region  to run tex on a region."
  85.   :type 'string
  86.   :group 'texinfo-tex)
  87.  
  88. (defcustom texinfo-texindex-command "texindex"
  89.   "*Command used by  texinfo-texindex  to sort unsorted index files."
  90.   :type 'string
  91.   :group 'texinfo-tex)
  92.  
  93. (defcustom texinfo-tex-dvi-print-command "lpr -d"
  94.   "*Command string used by \\[tex-print] to print a .dvi file."
  95.   :type 'string
  96.   :group 'texinfo-tex)
  97.  
  98. (defcustom texinfo-show-tex-queue-command "lpq"
  99.   "*Command string used to show the Texinfo TeX print queue.
  100. Command is used by \\[texinfo-show-tex-print-queue] and it
  101. should show the queue that \\[texinfo-tex-print] puts jobs on."
  102.   :type 'string
  103.   :group 'texinfo-tex)
  104.  
  105. (defcustom texinfo-delete-from-print-queue-command "lprm"
  106.   "*Command string used to delete a job from the line printer queue.
  107. Command is used by \\[texinfo-delete-from-tex-print-queue] based on
  108. number provided by a previous \\[texinfo-show-tex-print-queue]
  109. command."
  110.   :type 'string
  111.   :group 'texinfo-tex)
  112.  
  113. (defvar texinfo-tex-trailer "@bye"
  114.   "String appended after a region sent to TeX by texinfo-tex-region.")
  115.  
  116. (defvar texinfo-tex-original-file ""
  117.   "Original name of file on which to run TeX.")
  118.  
  119. (defvar texinfo-tex-temp-file nil
  120.   "Temporary file name used for text being sent as input to TeX.")
  121.  
  122. (defvar texinfo-tex-root-temp-file nil
  123.   "Temporary file name used for text being sent as input to TeX.")
  124.  
  125.  
  126. ;;; Texinfo TeX main functions
  127.  
  128. (defun texinfo-tex-region (beginning end)
  129.   "Run tex on the current region. 
  130.  
  131. A temporary file is written in the default directory, and tex is run
  132. in that directory.  The first line of the file is copied to the
  133. temporary file; and if the buffer has a header, it is written to the
  134. temporary file before the region itself.  The buffer's header is all
  135. lines between the strings defined by texinfo-start-of-header and
  136. texinfo-end-of-header inclusive.  The header must start in the first 100
  137. lines.  The value of texinfo-tex-trailer is appended to the temporary file
  138. after the region."
  139.   
  140.   (interactive "r")
  141.   (if (get-buffer "*texinfo-tex-shell*")
  142.       (quit-process (get-process "texinfo-tex-shell") t)
  143.     (texinfo-tex-start-shell))
  144.   
  145.   (setq texinfo-tex-root-temp-file
  146.         (expand-file-name 
  147.          (make-temp-name
  148.           (prin1-to-string (read (buffer-name))))))
  149.   
  150.   (let ((texinfo-tex-temp-file (concat texinfo-tex-root-temp-file ".tex")))
  151.     (save-excursion
  152.       (save-restriction
  153.         (widen)
  154.         (goto-char (point-min))
  155.         (forward-line 100)
  156.         (let ((search-end (point))
  157.               (header-beginning (point-min)) (header-end (point-min)))
  158.           (goto-char (point-min))
  159.           ;; Copy first line, the `\input texinfo' line, to temp file
  160.           (write-region (point) 
  161.                         (save-excursion (forward-line 1) (point))
  162.                         texinfo-tex-temp-file nil nil)
  163.           ;; Don't copy first line twice if region includes it.
  164.           (forward-line 1)
  165.           (if (< beginning  (point)) (setq beginning  (point)))
  166.           ;; Initialize the temp file with either the header or nothing
  167.           (if (search-forward texinfo-start-of-header search-end t)
  168.               (progn
  169.                 (beginning-of-line)
  170.                 (setq header-beginning (point)) ; Mark beginning of header.
  171.                 (if (search-forward texinfo-end-of-header nil t)
  172.                     (progn (beginning-of-line)
  173.                            (setq header-end (point)))   ; Mark end of header.
  174.                   (setq header-beginning (point-min))))) ; Else no header.
  175.           ;; Copy  header  to temp file.
  176.           (write-region
  177.            (min header-beginning beginning )
  178.            header-end
  179.            texinfo-tex-temp-file t nil)
  180.           ;; Copy  region  to temp file.
  181.           (write-region
  182.            (max beginning  header-end)
  183.            end
  184.            texinfo-tex-temp-file t nil)
  185.           ;; This is a kludge to insert the texinfo-tex-trailer into the
  186.           ;; texinfo-tex-temp-file.  We have to create a special buffer
  187.           ;; in which to insert the texinfo-tex-trailer first because there is
  188.           ;; no function with which to append a literal string directly
  189.           ;; to a file.
  190.           (let ((local-tex-trailer texinfo-tex-trailer)
  191.                 (temp-buffer (get-buffer-create " texinfo-trailer-buffer")))
  192.             (set-buffer temp-buffer)
  193.             (erase-buffer)
  194.             ;; make sure trailer isn't hidden by a comment
  195.             (insert-string "\n")
  196.             (if local-tex-trailer (insert local-tex-trailer))
  197.             (write-region (point-min) (point-max) 
  198.                           texinfo-tex-temp-file t nil)))
  199.         (set-process-sentinel (get-process "texinfo-tex-shell") 
  200.                               'texinfo-tex-shell-sentinel)
  201.         (send-string "texinfo-tex-shell"
  202.                      (concat texinfo-tex-shell-cd-command " "
  203.                              default-directory "\n"))
  204.         (send-string "texinfo-tex-shell"
  205.                      (concat texinfo-tex-command " "
  206.                              texinfo-tex-temp-file "\n  "))
  207.         (texinfo-recenter-tex-output-buffer 0)))))
  208.  
  209. (defun texinfo-tex-buffer (buffer)
  210.   "Run TeX on current buffer.
  211. After running TeX the first time, you may have to run \\[texinfo-texindex]
  212. and then \\[texinfo-tex-buffer] again."
  213.   (interactive 
  214.    (list
  215.     ;; Sometimes you put point into *texinfo-tex-shell*; this prompts
  216.     ;; you for the correct file regardless.
  217.     (if (and 
  218.          (string= (buffer-name (current-buffer)) "*texinfo-tex-shell*")
  219.          texinfo-tex-root-temp-file)
  220.         (read-string (format "Run TeX on: ")
  221.                      texinfo-tex-original-file)
  222.       (read-string (format "Run TeX on: ") (buffer-name (current-buffer))))))
  223.   
  224.   ;; Set to original buffer if in *texinfo-tex-shell*; otherwise,
  225.   ;; record name of current buffer.
  226.   (if (string= (buffer-name (current-buffer)) "*texinfo-tex-shell*")
  227.       (set-buffer buffer)
  228.     (setq texinfo-tex-original-file
  229.            (buffer-name (current-buffer))))
  230.  
  231.   (if (get-buffer "*texinfo-tex-shell*")
  232.       (quit-process (get-process "texinfo-tex-shell") t)
  233.     (texinfo-tex-start-shell))
  234.   (cond ((null buffer-file-name)
  235.          (error "Buffer not visiting any file!"))
  236.         ((buffer-modified-p)
  237.          (error "Buffer has been modified since last saved!"))
  238.         (t (set-process-sentinel (get-process "texinfo-tex-shell") 
  239.                                  'texinfo-tex-shell-sentinel)
  240.            (send-string "texinfo-tex-shell"
  241.                         (concat texinfo-tex-shell-cd-command 
  242.                                 " "
  243.                                 (file-name-directory
  244.                                  (buffer-file-name
  245.                                   (get-buffer buffer)))
  246.                                 "\n"))
  247.            (send-string "texinfo-tex-shell"
  248.                         (concat texinfo-tex-command " " buffer "\n  "))
  249.            
  250.            ;; so the texinfo-tex-print command works
  251.            (setq texinfo-tex-root-temp-file
  252.                  (substring buffer 0
  253.                             (or (string-match "\\.tex" buffer)
  254.                                 (length buffer))))
  255.            
  256.            (texinfo-recenter-tex-output-buffer 0))))
  257.  
  258. (defun texinfo-texindex ()
  259.   "Run texindex on unsorted index files.
  260. The index files are made by \\[texinfo-tex-region] or \\[texinfo-tex-buffer].
  261. Runs the shell command defined by texinfo-texindex-command."
  262.   (interactive)
  263.   (send-string "texinfo-tex-shell"
  264.                (concat texinfo-texindex-command
  265.                        " " texinfo-tex-root-temp-file ".??" "\n"))
  266.   (texinfo-recenter-tex-output-buffer nil))
  267.  
  268. (defun texinfo-tex-print ()
  269.   "Print .dvi file made by \\[texinfo-tex-region] or \\[texinfo-tex-buffer].
  270. Runs the shell command defined by texinfo-tex-dvi-print-command."
  271.   (interactive)
  272.   (send-string "texinfo-tex-shell"
  273.                (concat texinfo-tex-dvi-print-command
  274.                        " " texinfo-tex-root-temp-file ".dvi" "\n"))
  275.   (texinfo-recenter-tex-output-buffer nil))
  276.  
  277.  
  278. ;;; Texinfo TeX utility functions
  279.  
  280. (defun texinfo-tex-start-shell ()
  281.   (save-excursion
  282.     (require 'texinfo)
  283.     (set-buffer (if (fboundp 'make-shell)
  284.             (make-shell "texinfo-tex-shell" "/bin/sh" nil "-v")
  285.           (make-comint "texinfo-tex-shell" "/bin/sh" nil "-v")))
  286.     (setq texinfo-tex-shell-map (copy-keymap shell-mode-map))
  287.     (texinfo-define-common-keys texinfo-tex-shell-map)
  288.     (use-local-map texinfo-tex-shell-map)
  289.     (run-hooks 'texinfo-tex-shell-hook)
  290.     (if (zerop (buffer-size))
  291.         (sleep-for 1))))
  292.  
  293. (defun texinfo-quit-tex-job ()
  294.   "Quit currently running TeX job, by sending an `x' to it."
  295.   (interactive)
  296.   (if (not (get-process "texinfo-tex-shell"))
  297.       (error "No TeX shell running."))
  298.   (save-excursion
  299.     (set-buffer (get-buffer "*texinfo-tex-shell*"))
  300.     (goto-char (point-max))
  301.     (insert "x")
  302.     (shell-send-input)))
  303.  
  304. (defun texinfo-kill-tex-job ()
  305.   "Kill the currently running TeX job."
  306.   (interactive)
  307.   (if (get-process "texinfo-tex-shell")
  308.         ;; Use `texinfo-tex-shell-sentinel' to restart
  309.         ;; texinfo-tex-shell after it is killed.
  310.         (kill-process (get-process "texinfo-tex-shell"))))
  311.  
  312. (defun texinfo-tex-shell-sentinel (process event)
  313.   "Restart texinfo-tex-shell after it is killed."
  314.   (if (equal event "killed\n")
  315.       (save-excursion
  316.         (set-buffer  "*texinfo-tex-shell*")
  317.         (insert "\n")
  318.         (texinfo-tex-start-shell))))
  319.  
  320. (defun texinfo-recenter-tex-output-buffer (linenum)
  321.   "Redisplay buffer of TeX job output so that most recent output can be seen.
  322. The last line of the buffer is displayed on
  323. line LINE of the window, or centered if LINE is nil."
  324.   (interactive "P")
  325.   (let ((texinfo-tex-shell (get-buffer "*texinfo-tex-shell*"))
  326.         (old-buffer (current-buffer)))
  327.     (if (null texinfo-tex-shell)
  328.         (message "No TeX output buffer")
  329.       (pop-to-buffer texinfo-tex-shell)
  330.       (bury-buffer texinfo-tex-shell)
  331.       (goto-char (point-max))
  332.       (recenter (if linenum
  333.                     (prefix-numeric-value linenum)
  334.                   (/ (window-height) 2)))
  335.       (pop-to-buffer old-buffer)
  336.       )))
  337.  
  338. (defun texinfo-show-tex-print-queue ()
  339.   "Show the print queue that \\[texinfo-tex-print] put your job on.
  340. Runs the shell command defined by texinfo-show-tex-queue-command."
  341.   (interactive)
  342.   (if (not (texinfo-tex-shell-running-p))
  343.       (texinfo-tex-start-shell))
  344.   (send-string "texinfo-tex-shell"
  345.                (concat texinfo-show-tex-queue-command "\n"))
  346.   (texinfo-recenter-tex-output-buffer nil))
  347.  
  348. (defun texinfo-delete-from-tex-print-queue (job-number)
  349.   "Delete job from the line printer spooling queue.
  350. You are prompted for the job number (shown by a previous
  351. \\[texinfo-show-tex-print-queue] command."
  352.   (interactive "nPrinter job number for deletion: ")
  353.   (if (texinfo-tex-shell-running-p)
  354.       (texinfo-kill-tex-job)
  355.     (texinfo-tex-start-shell))
  356.   (send-string "texinfo-tex-shell"
  357.                (concat 
  358.                 texinfo-delete-from-print-queue-command
  359.                 " "
  360.                 job-number"\n"))
  361.   (texinfo-recenter-tex-output-buffer nil))
  362.  
  363. (defun texinfo-tex-shell-running-p ()
  364.   (and (get-process "texinfo-tex-shell")
  365.        (eq (process-status (get-process "texinfo-tex-shell")) 'run)))
  366.  
  367.  
  368. ;;; Place `provide' at end of file.
  369. (provide 'texnfo-tex)
  370. ;;;;;;;;;;;;;;;; end texnfo-tex.el ;;;;;;;;;;;;;;;;
  371.